package chapter7;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则对象的reset方法可以将现有的Matcher对象应用于新的字符序列
 */
public class MatchesTest {
    public static void main(String[] args) {
        String[] mails = new String[] {
                "leemz@126.com",
                "wussrc@126.gov",
                "lmz8860@gmail.org",
                "wussrc@21cn.xx"
        };
        String mailReg = "\\w{3,20}@\\w+\\.(com|gov|cn|net|org)";
        Pattern mailPattern = Pattern.compile(mailReg);
        Matcher matcher = null;
        for(String mail : mails) {
            if(matcher == null) {
                matcher = mailPattern.matcher(mail);
            } else {
                matcher.reset(mail);
            }
            String result = mail + (matcher.matches() ? "是" : "不是") + "一个有效的邮件地址";
            System.out.println(result);
        }
    }
}
